package com.lykj.llw.box.ui.diafrg

import android.os.Bundle
import android.util.DisplayMetrics
import android.view.*
import android.widget.RelativeLayout
import com.blankj.utilcode.util.SizeUtils
import com.lykj.lib_base.base.BaseDiaFrg
import com.lykj.llw.box.databinding.GeNcDiafrgDownProgressBinding
import me.jessyan.autosize.utils.AutoSizeUtils
import kotlin.properties.Delegates

///
///下载进度条弹窗
///
class GEDownloadProgressDiaFrg : BaseDiaFrg() {
    var binding: GeNcDiafrgDownProgressBinding? = null
    lateinit var layoutParams: RelativeLayout.LayoutParams
    var width by Delegates.notNull<Int>()

    override fun onStart() {
        super.onStart()

        val window = dialog!!.window
        //设置窗口弹出动画
        window!!.setWindowAnimations(0)
        val layoutParams = window.attributes
        layoutParams.dimAmount = 0.75f
        layoutParams.gravity = Gravity.CENTER
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT

        if (null != dialog) {
            val dm = DisplayMetrics()
            requireActivity().windowManager.defaultDisplay.getMetrics(dm)
            dialog!!.setCancelable(false)
            dialog!!.setCanceledOnTouchOutside(false)
            dialog!!.window!!.setGravity(Gravity.CENTER)
            dialog!!.window!!.setLayout(
                AutoSizeUtils.dp2px(requireContext(), 300f),
                AutoSizeUtils.dp2px(requireContext(), 150f)
            )
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = GeNcDiafrgDownProgressBinding.inflate(layoutInflater)
        val view = binding?.root

        initUI()
        initData()
        setListener()

        return view
    }

    open fun setProgress(progress: Int) {
        activity?.runOnUiThread {
            if (binding != null) {
                binding!!.tvProgress.text = "$progress/100"
                layoutParams.width = width * progress / 100
                binding!!.vProgress.layoutParams = layoutParams
            }
        }
    }

    override fun initUI() {
        layoutParams = binding!!.vProgress.layoutParams as RelativeLayout.LayoutParams
        width = SizeUtils.dp2px(300f - 12f - 12f)
    }

    override fun initData() {
    }

    override fun setListener() {
    }
}